package Dao;


import models.IUser;
import models.User;
import org.apache.ibatis.session.SqlSession;
import java.util.Date;


/**
 * Created by 郑凯 on 2016/6/12.
 */
public class LoginAndRegister {

    public static  boolean getUserByName(String name)
    {
        SqlSession sqlSession=  mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        User user = new User(); user.setName(name);
        User u= iUser.GetUserByName(user);
        sqlSession.commit();
        sqlSession.close();
        if(u == null) return  false;
        else return true;
    }
    public static  boolean getUserByEmail(String email){
        SqlSession sqlSession=  mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        User user = new User(); user.setEmail(email);
        User u= iUser.GetUserByEmail(user);
        sqlSession.commit();
        sqlSession.close();
        if(u == null) return  false;
        else return true;

    }
    public static Object login(String name,String password){
        SqlSession sqlSession=  mybatis_GetSessionFactory.getSqlSessionFactory().openSession();

        User user = new User();
        user.setName(name);
        user.setEmail(name);
        user.setPassword(password);

        User u= sqlSession.selectOne("UserMapper.User.GetUserByNameAndEmail",user);
        sqlSession.commit();
        sqlSession.close();
        return u;
    }


    public static boolean register(String name,String password,String email){
        SqlSession sqlSession=  mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        User user = new User();
        user.setUid(String.valueOf(new Date().getTime()));
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);

        int a = sqlSession.insert("UserMapper.User.InsertUser",user);
        sqlSession.commit();
        sqlSession.close();
        return true;
    }

}
